
let TempState,TempInfo,MonthDays,YearDays;
let todays,weeks,months,years,allyears;
layui.config({
    version:'1510164124181'
}).use(['form', 'laypage', 'layer', 'element'],function(){
    form = layui.form;
    layer = layui.layer;
    $ = layui.jquery;
    laypage = layui.laypage;//分页
    element = layui.element; //元素操作

    TempState=$("body").attr("data-id");
    TempInfo=$("body").attr("data-info");

    if(TempInfo=='shopcart'){
        SelectCollectList(pageNo,pageRecords,TempState);
    }else if(TempInfo=='orderlist'){
        SelectOrderList(pageNo,pageRecords,TempState);
    }else{

        findOperateData();
        SelectProInfo();
    }


    MonthDays=SelectThisDays();
    YearDays=SelectYearInfo();//8年


    let RightTargetInfo = $.session.get("RightTargetInfo");
    $("header .title[data-info="+RightTargetInfo+"]").show();

});

function SelectProInfo() {
    let LogisticePro=$(".LogisticePro").val();
    $("#tableInfo tbody").html("");
    $.ajax({
        url: httpurl+"/"+LogisticePro,
        cache: false,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        async:false,
        data:{
            "page":pageNo,
            "pagesize":pageRecords,
        },
        success: function(data) {
            let dataHtml="";
            if(data.rtnCode==200){
                let initdata=data.data;
                totalRecords =initdata[initdata.length-1].total;
                for(let i=0;i<initdata.length;i++){
                    dataHtml+='<tr class="text-primary">'+
                                    '<td>'+initdata[i].productName+'</td>'+
                                    '<td>'+initdata[i].num+'</td>'+
                                    '<td>'+initdata[i].money+'</td>'+
                                '</tr>';
                }
            }else{
                dataHtml += '<tr>'
                    +'<td colspan="12" style="text-align:center;">暂无数据</td>'
                    +'</tr>';
            }
            $("#tableInfo tbody").html(dataHtml);
            layer.closeAll();
            page();
        }
    });
}



function SearchInfo(){
    pageNo=1;
    inituserdata();
}

//分页
function page(){
    laypage.render({
        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
        ,count:totalRecords //数据总数，从服务端得到
        ,curr: pageNo //获取起始页 location.hash.replace('#!fenye=', '')
        ,hash: 'fenye' //自定义hash值
        ,layout: ['count', 'prev', 'page', 'next', 'skip']
        ,jump: function(obj, first){
            //首次不执行
            if(!first){
                pageNo = obj.curr;
                pageRecords = obj.limit;// 当前页面记录数
                inituserdata();
            }
        }
    });
}




function findOperateData() {
    $.ajax({
        url: httpurl + "/findOperateData",
        cache: false,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        success: function (data) {
            console.log("findOperateData====" + JSON.stringify(data));
            let dataInfo=data.data;
            let AllcountSale=parseFloat(dataInfo.AllcountSale).toFixed(2);//查销量
            let Allsum=parseFloat(dataInfo.Allsum).toFixed(2);//查询营业额
            let Allvisit=parseFloat(dataInfo.Allvisit).toFixed(2);////查询访问量

            $(".AllcountSale").html("总销售量："+AllcountSale+"条");
            $(".Allsum").html("总营业额："+Allsum+"元");
            $(".Allvisit").html("总访问量："+Allvisit+"条");

            HandleData(dataInfo);
        }
    });
}

function HandleData(dataInfo){
    let countSaleDays=dataInfo.countSaleDays;//查当天销量
    let sumDays=dataInfo.sumDays;//查询当天营业额

    let sumWeeks=dataInfo.sumWeeks;////查询本周销量
    let countSaleWeeks=dataInfo.countSaleWeeks;//查询本周营业额

    let sumMonths=dataInfo.sumMonths;////查询本月销量
    let countSaleMonths=dataInfo.countSaleMonths;//查询本月营业额

    let sumYears=dataInfo.sumYears;////查询本年销量
    let countSaleYears=dataInfo.countSaleYears;//查询本年营业额

    let sumAlls=dataInfo.sumAlls;////查询所有销量
    let countSaleAlls=dataInfo.countSaleAlls;//查询所有营业额

    let tempInfo;
    let Vals;

    let sumWeeksArr=[0,0,0,0,0,0,0];//查询本周销量
    for(let i=0;i<sumWeeks.length;i++){
        tempInfo = parseInt(sumWeeks[i].keyInfo);
        Vals=sumWeeks[i].valInfo;
        sumWeeksArr[tempInfo]=(sumWeeks[i].valInfo==null?0:parseFloat(sumWeeks[i].valInfo));
    }

    let countSaleWeeksArr=[0,0,0,0,0,0,0];//查询本周营业额
    for(let i=0;i<countSaleWeeks.length;i++){
        tempInfo = parseInt(countSaleWeeks[i].keyInfo);
        countSaleWeeksArr[tempInfo]=(countSaleWeeks[i].valInfo==null?0:parseFloat(countSaleWeeks[i].valInfo));
    }

    let sumMonthsArr=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];//查询本月销量
    for(let i=0;i<sumMonths.length;i++){
        tempInfo = parseInt(sumMonths[i].keyInfo);
        sumMonthsArr[tempInfo]=(sumMonths[i].valInfo==null?0:parseFloat(sumMonths[i].valInfo));
    }

    let countSaleMonthsArr=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];//查询本月营业额
    for(let i=0;i<countSaleMonths.length;i++){
        tempInfo = parseInt(countSaleMonths[i].keyInfo);
        countSaleMonthsArr[tempInfo]=(countSaleMonths[i].valInfo==null?0:parseFloat(countSaleMonths[i].valInfo));
    }

    let sumYearsArr=[0,0,0,0,0,0,0,0,0,0,0,0];//查询本年销量
    for(let i=0;i<sumYears.length;i++){
        tempInfo = parseInt(sumYears[i].keyInfo);
        sumYearsArr[tempInfo]=(sumYears[i].valInfo==null?0:parseFloat(sumYears[i].valInfo));
    }

    let countSaleYearsArr=[0,0,0,0,0,0,0,0,0,0,0,0];//查询本年营业额
    for(let i=0;i<countSaleYears.length;i++){
        tempInfo = parseInt(countSaleYears[i].keyInfo);
        countSaleYearsArr[tempInfo]=(countSaleYears[i].valInfo==null?0:parseFloat(countSaleYears[i].valInfo));
    }

    let sumAllsArr=[0,0,0,0,0,0,0,0,0,0,0];//查询所有销量
    for(let i=0;i<sumAlls.length;i++){
        tempInfo = parseInt(sumAlls[i].keyInfo);
        sumAllsArr[tempInfo]=(sumAlls[i].valInfo==null?0:parseFloat(sumAlls[i].valInfo));
    }

    let countSaleAllsArr=[0,0,0,0,0,0,0,0,0,0,0];//查询所有营业额
    for(let i=0;i<countSaleAlls.length;i++){
        tempInfo = parseInt(countSaleAlls[i].keyInfo);
        countSaleAllsArr[tempInfo]=(countSaleAlls[i].valInfo==null?0:parseFloat(countSaleAlls[i].valInfo));
    }
    todays=[{
        name: '销售量',
        type: 'bar',
        data: [countSaleDays]
    },{
        name: '营业额',
        type: 'bar',
        data: [sumDays]
    }];
    weeks=[{
        name: '销售量',
        type: 'bar',
        data: sumWeeksArr
    },{
        name: '营业额',
        type: 'bar',
        data: countSaleWeeksArr
    }];

    months=[{
        name: '销售量',
        type: 'bar',
        data: sumMonthsArr
    },{
        name: '营业额',
        type: 'bar',
        data: countSaleMonthsArr
    }];

    years=[{
        name: '销售量',
        type: 'bar',
        data: sumYearsArr
    },{
        name: '营业额',
        type: 'bar',
        data: countSaleYearsArr
    }];

    allyears=[{
        name: '销售量',
        type: 'bar',
        data: sumAllsArr
    },{
        name: '营业额',
        type: 'bar',
        data: countSaleAllsArr
    }];
    recharge();
}
function recharge() {
    // console.log(JSON.stringify(MonthDays)+"====MonthDays"+JSON.stringify(months));
    // 基于准备好的dom，初始化echarts实例
    let myChart = echarts.init(document.getElementById('main'));
    // 指定图表的配置项和数据
    let option = {
        title: {
            text: '图表'
        },
        tooltip: {},
        legend: {
            data:['销售量','营业额']
        },
        // xAxis: {
        //     data: ["1","2","3","4","6","6","7","8","9","10","11","12"]
        // },
        yAxis: {
            title: {
                text: '万'
            },
        }
    };

    let recharges=$("#search_type").find("option:selected").val();
    if(recharges=='1'){
        option.xAxis={data: ["今日"]};
        option.series=todays;
    }else if(recharges=='2'){
        option.xAxis={data: ["周一","周二","周三","周四","周五","周六","周七"]};
        option.series=weeks;
    }else if(recharges=='3'){
        option.xAxis={data: MonthDays};
        option.series=months;
    }else if(recharges=='4'){
        option.xAxis={data: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]};
        option.series=years;
    }else if(recharges=='5'){
        option.xAxis={data: YearDays};
        option.series=allyears;
    }
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);

}
